package com.gxa.common;

import com.gxa.common.R;
import com.gxa.common.jwt.JwtResult;
import com.gxa.common.jwt.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created with IntelliJ IDEA.
 * User: 赵贺益
 * Date: 2021/11/30 11:37
 * Description:
 * Version: V1.0
 */
public class MyInterceptor implements HandlerInterceptor {
//    @Autowired
//    private JwtUtils jwtUtils;
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object o) throws Exception {
       String token= request.getHeader("token");
////        System.out.println("请求地址"+request.getRequestURI());
////        System.out.println("请求方式"+request.getMethod());
////        System.out.println("============================================"+token);
////        System.out.println(token);
        if (HttpMethod.OPTIONS.toString().equals(request.getMethod())) {
            System.out.println("OPTIONS请求，放行");
            return true;
        }
//        if(request.getRequestURL())
//        System.out.println(request.getRequestURL());
//        System.out.println(request.getRequestURI());

//        if(HttpMethod.GET.toString().equals(request.getMethod())){
//            System.out.println("GET请求，放行");
//            return true;
//        }

       if(" ".equals(token)||token==null){
           R r=new R();
           r.setMsg("请先登录");
           r.setCode(404);
           return  false;
       }else {
//           System.out.println("9999999999999999999999");
         //验证token是否正确
           JwtResult result=JwtUtils.validateJwt(token);
           if(result.isSuccess()){

               return  true;
           }else {
//               System.out.println("88888888888888888888");
               result.getErrCode();
               return false;
           }
//
       }

//        return true;
    }

    @Override
    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {

    }
}
